<template>
  <el-dialog
    :title="!dataForm.userCode ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="用户类型" prop="userCategory">
      <el-select v-model="dataForm.userCategory" placeholder="请选择用户类型" style="width: 100%;">
        <el-option
          v-for="item in userCategoryOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="手机号码" prop="userPhone">
      <el-input v-model="dataForm.userPhone" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="clearData">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        makeSure: false,
        dataForm: {
          userCode: '',
          userName: '',
          userCategory: '',
          userPhone: '',
        },
        userCategoryOptions: [
          { value: 'ADMIN', label: '管理员' },
          { value: 'NORMAL', label: '设计师' }
        ],
        dataRule: {

          userName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          userCategory: [
            { required: true, message: '请选择用户类型', trigger: 'change' }
          ],
          userPhone: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          userThirdAccount: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (userCode) {
       this.dataForm.userCode = userCode || undefined
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userCode) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/queryWormholeGalaxyUser`),
              method: 'post',
              data: this.$http.adornData({
                'userCode':userCode,
              })
            }).then(({data}) => {
              if (data && data.success) {
                this.dataForm.userCode = data.data.userCode
                this.dataForm.userName = data.data.userName
                this.dataForm.userCategory = data.data.userCategory
                this.dataForm.userPhone = data.data.userPhone
              }
            })
          }
        })
      },
      clearData() {
        this.visible = false
        this.$emit('refreshDataList')
      },
      // 表单提交
      dataFormSubmit () {
        this.submitFormData();
      },
      // 提交表单数据
      submitFormData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/${!this.dataForm.userCode ? 'addWormholeGalaxyUser' : 'updateWormholeGalaxyUser'}`),
              method: 'post',
              data: this.$http.adornData({
                'userCode': this.dataForm.userCode,
                'userName': this.dataForm.userName,
                'userPassword':this.dataForm.userPassword,
                'userCategory': this.dataForm.userCategory,
                'userPhone': this.dataForm.userPhone,
              })
            }).then(({data}) => {
              if (data && data.success) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error("系统异常")
              }
            })
          }
        })
      }
    }

  }
</script>

<style scoped>
.upload-demo {
  width: 100%;
}

.preview-image img {
  max-width: 100%;
  height: auto;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  padding: 5px;
}
</style>
